package org.reos.ReGenerator;

import org.reos.ReGenConfigData.ParserDataCore;
import org.reos.ReParser.ReParser;
import org.reos.ReGenConfigData.SystemSection;
import org.stringtemplate.v4.*;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class ReGenerator {

    private STGroup src_group;
    private ST      src_templ;
    private STGroup inc_group;
    private ST      inc_templ;

    String srcOut;
    String headerOut;

    public ReGenerator() {

        //src_group = new STGroupFile("..\\ReTest\\source.stg");
        src_group = new STGroupFile("tpl/source.stg");      // .stg file is in the same jar
        src_templ = src_group.getInstanceOf("reos_source_template");

        //inc_group = new STGroupFile("..\\ReTest\\header.stg");
        inc_group = new STGroupFile("tpl/header.stg");      // .stg file is in the same jar
        inc_templ = inc_group.getInstanceOf("reos_header_template");

    }

    public void FillData(ArrayList<ParserDataCore> parserData) {
        for (ParserDataCore dataUnit : parserData){
            src_templ.add(dataUnit.getDataType(), dataUnit);
            inc_templ.add(dataUnit.getDataType(), dataUnit);

            if (dataUnit.getDataType().equals(ReParser.ParserDataType.SYSTEM.toString())) {
                srcOut = ((SystemSection.ParserDataSystem)dataUnit).SOURCE_OUT;
                headerOut = ((SystemSection.ParserDataSystem)dataUnit).HEADER_OUT;
            }
        }
    }

    public void Generate(ArrayList<ParserDataCore> parserData) {
        FillData(parserData);
//        System.out.println(src_templ.render());
//        System.out.println(inc_templ.render());

        // TODO make it more sophisticated
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(srcOut, "UTF-8");
            writer.println(src_templ.render());
            writer.close();
            writer = new PrintWriter(headerOut, "UTF-8");
            writer.println(inc_templ.render());
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        //st.inspect();
    }
}

//class TASK {
//    public String NAME;
//    public String CLASS;
//    public int PRIO;
//    public int STACK;
//}
//
//class EVENT {
//    public String NAME;
//}
//
//class Message {
//    public String NAME;
//}
//
//class ISR {
//    public String HANDLER;
//    public String TYPE;
//    public String VECTOR;
//}
//
//class SYSTEM {
//    public String DATE;
//    public String SOURCE_OUT;
//    public String HEADER_OUT;
//    SYSTEM() {
//        DATE = new Date().toString();
//    }
//}